{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from pandas import DataFrame,Series\n",
    "import pandas as pd\n",
    "import numpy as np\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "data": {
      "text/plain": "     Python  Java  H5  Pandas\n鲁班       47    92  81      24\n张三丰      23    42   0      98\n张无忌      37     3  39      28\n杜甫       13    20  20      59\n李白       48    83  90      36",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Python</th>\n      <th>Java</th>\n      <th>H5</th>\n      <th>Pandas</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>鲁班</th>\n      <td>47</td>\n      <td>92</td>\n      <td>81</td>\n      <td>24</td>\n    </tr>\n    <tr>\n      <th>张三丰</th>\n      <td>23</td>\n      <td>42</td>\n      <td>0</td>\n      <td>98</td>\n    </tr>\n    <tr>\n      <th>张无忌</th>\n      <td>37</td>\n      <td>3</td>\n      <td>39</td>\n      <td>28</td>\n    </tr>\n    <tr>\n      <th>杜甫</th>\n      <td>13</td>\n      <td>20</td>\n      <td>20</td>\n      <td>59</td>\n    </tr>\n    <tr>\n      <th>李白</th>\n      <td>48</td>\n      <td>83</td>\n      <td>90</td>\n      <td>36</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "index = ['鲁班', '张三丰', '张无忌', '杜甫', '李白']\n",
    "columns = ['Python', 'Java', 'H5', 'Pandas']\n",
    "data = np.random.randint(0, 100, size=(5, 4))\n",
    "\n",
    "df = pd.DataFrame(data=data, index=index, columns=columns)\n",
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "data": {
      "text/plain": "     Python  Java  H5  Pandas\n鲁班      100   100  81      24\n张三丰      23    42   0      98\n张无忌      37     3  39      28\n杜甫       13    20  20      59\n李白       48    83  90      36",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Python</th>\n      <th>Java</th>\n      <th>H5</th>\n      <th>Pandas</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>鲁班</th>\n      <td>100</td>\n      <td>100</td>\n      <td>81</td>\n      <td>24</td>\n    </tr>\n    <tr>\n      <th>张三丰</th>\n      <td>23</td>\n      <td>42</td>\n      <td>0</td>\n      <td>98</td>\n    </tr>\n    <tr>\n      <th>张无忌</th>\n      <td>37</td>\n      <td>3</td>\n      <td>39</td>\n      <td>28</td>\n    </tr>\n    <tr>\n      <th>杜甫</th>\n      <td>13</td>\n      <td>20</td>\n      <td>20</td>\n      <td>59</td>\n    </tr>\n    <tr>\n      <th>李白</th>\n      <td>48</td>\n      <td>83</td>\n      <td>90</td>\n      <td>36</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.replace({47:100,92:100})"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "data": {
      "text/plain": "鲁班     100.0\n张三丰      NaN\n张无忌      NaN\n杜甫       NaN\n李白       NaN\nName: Python, dtype: float64"
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# map是Series调用，不能使用DataFrame调用\n",
    "df[\"Python\"].map({47:100})"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "data": {
      "text/plain": "鲁班     49\n张三丰    25\n张无忌    39\n杜甫     15\n李白     50\nName: Python, dtype: int64"
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[\"Python\"].map(lambda x:x+2)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "data": {
      "text/plain": "     Python  Java  H5  Pandas  Python_new\n鲁班       47    92  81      24          49\n张三丰      23    42   0      98          25\n张无忌      37     3  39      28          39\n杜甫       13    20  20      59          15\n李白       48    83  90      36          50",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Python</th>\n      <th>Java</th>\n      <th>H5</th>\n      <th>Pandas</th>\n      <th>Python_new</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>鲁班</th>\n      <td>47</td>\n      <td>92</td>\n      <td>81</td>\n      <td>24</td>\n      <td>49</td>\n    </tr>\n    <tr>\n      <th>张三丰</th>\n      <td>23</td>\n      <td>42</td>\n      <td>0</td>\n      <td>98</td>\n      <td>25</td>\n    </tr>\n    <tr>\n      <th>张无忌</th>\n      <td>37</td>\n      <td>3</td>\n      <td>39</td>\n      <td>28</td>\n      <td>39</td>\n    </tr>\n    <tr>\n      <th>杜甫</th>\n      <td>13</td>\n      <td>20</td>\n      <td>20</td>\n      <td>59</td>\n      <td>15</td>\n    </tr>\n    <tr>\n      <th>李白</th>\n      <td>48</td>\n      <td>83</td>\n      <td>90</td>\n      <td>36</td>\n      <td>50</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[\"Python_new\"] = df[\"Python\"].map(lambda x:x+2)\n",
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "data": {
      "text/plain": "     Python  Java  H5  Pandas  Python_new Python_grade\n鲁班       47    92  81      24          49          不及格\n张三丰      23    42   0      98          25          不及格\n张无忌      37     3  39      28          39          不及格\n杜甫       13    20  20      59          15          不及格\n李白       48    83  90      36          50          不及格",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Python</th>\n      <th>Java</th>\n      <th>H5</th>\n      <th>Pandas</th>\n      <th>Python_new</th>\n      <th>Python_grade</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>鲁班</th>\n      <td>47</td>\n      <td>92</td>\n      <td>81</td>\n      <td>24</td>\n      <td>49</td>\n      <td>不及格</td>\n    </tr>\n    <tr>\n      <th>张三丰</th>\n      <td>23</td>\n      <td>42</td>\n      <td>0</td>\n      <td>98</td>\n      <td>25</td>\n      <td>不及格</td>\n    </tr>\n    <tr>\n      <th>张无忌</th>\n      <td>37</td>\n      <td>3</td>\n      <td>39</td>\n      <td>28</td>\n      <td>39</td>\n      <td>不及格</td>\n    </tr>\n    <tr>\n      <th>杜甫</th>\n      <td>13</td>\n      <td>20</td>\n      <td>20</td>\n      <td>59</td>\n      <td>15</td>\n      <td>不及格</td>\n    </tr>\n    <tr>\n      <th>李白</th>\n      <td>48</td>\n      <td>83</td>\n      <td>90</td>\n      <td>36</td>\n      <td>50</td>\n      <td>不及格</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def g(grade):\n",
    "\tif grade< 60:\n",
    "\t\treturn \"不及格\"\n",
    "\telse:\n",
    "\t\treturn \"及格\"\n",
    "df[\"Python_grade\"] = df[\"Python\"].map(g)\n",
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "data": {
      "text/plain": "     Python  Java  大数据  Pandas  Python_new Python_grade\n鲁班       47    92   81      24          49          不及格\n张三丰      23    42    0      98          25          不及格\n张无忌      37     3   39      28          39          不及格\n杜甫       13    20   20      59          15          不及格\n李白       48    83   90      36          50          不及格",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Python</th>\n      <th>Java</th>\n      <th>大数据</th>\n      <th>Pandas</th>\n      <th>Python_new</th>\n      <th>Python_grade</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>鲁班</th>\n      <td>47</td>\n      <td>92</td>\n      <td>81</td>\n      <td>24</td>\n      <td>49</td>\n      <td>不及格</td>\n    </tr>\n    <tr>\n      <th>张三丰</th>\n      <td>23</td>\n      <td>42</td>\n      <td>0</td>\n      <td>98</td>\n      <td>25</td>\n      <td>不及格</td>\n    </tr>\n    <tr>\n      <th>张无忌</th>\n      <td>37</td>\n      <td>3</td>\n      <td>39</td>\n      <td>28</td>\n      <td>39</td>\n      <td>不及格</td>\n    </tr>\n    <tr>\n      <th>杜甫</th>\n      <td>13</td>\n      <td>20</td>\n      <td>20</td>\n      <td>59</td>\n      <td>15</td>\n      <td>不及格</td>\n    </tr>\n    <tr>\n      <th>李白</th>\n      <td>48</td>\n      <td>83</td>\n      <td>90</td>\n      <td>36</td>\n      <td>50</td>\n      <td>不及格</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 替换索引\n",
    "df3 = df.copy()\n",
    "df3.rename({\"鲁班\":\"鲁班大师\"})\n",
    "df3.rename({\"H5\":\"大数据\"},axis=1)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "df3.rename(index={'鲁班': \"Mr Lu\"})  # 更改行索引\n",
    "df3.rename(columns={'Python': 'PYTHON'})  # 更改列索引\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [
    {
     "data": {
      "text/plain": "     Python  Java  H5  Pandas  Python_new Python_grade\n鲁班       47    92  81      24          49          不及格\n张三丰      23    42   0      98          25          不及格\n张无忌      37     3  39      28          39          不及格\n杜甫       13    20  20      59          15          不及格\n李白       48    83  90      36          50          不及格",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Python</th>\n      <th>Java</th>\n      <th>H5</th>\n      <th>Pandas</th>\n      <th>Python_new</th>\n      <th>Python_grade</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>鲁班</th>\n      <td>47</td>\n      <td>92</td>\n      <td>81</td>\n      <td>24</td>\n      <td>49</td>\n      <td>不及格</td>\n    </tr>\n    <tr>\n      <th>张三丰</th>\n      <td>23</td>\n      <td>42</td>\n      <td>0</td>\n      <td>98</td>\n      <td>25</td>\n      <td>不及格</td>\n    </tr>\n    <tr>\n      <th>张无忌</th>\n      <td>37</td>\n      <td>3</td>\n      <td>39</td>\n      <td>28</td>\n      <td>39</td>\n      <td>不及格</td>\n    </tr>\n    <tr>\n      <th>杜甫</th>\n      <td>13</td>\n      <td>20</td>\n      <td>20</td>\n      <td>59</td>\n      <td>15</td>\n      <td>不及格</td>\n    </tr>\n    <tr>\n      <th>李白</th>\n      <td>48</td>\n      <td>83</td>\n      <td>90</td>\n      <td>36</td>\n      <td>50</td>\n      <td>不及格</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#apply()函数：既支持 Series，也支持 DataFrame\n",
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "outputs": [
    {
     "data": {
      "text/plain": "     Python  Java  H5  Pandas  Python_new Python_grade\n鲁班       57    92  81      24          49          不及格\n张三丰      33    42   0      98          25          不及格\n张无忌      47     3  39      28          39          不及格\n杜甫       23    20  20      59          15          不及格\n李白       58    83  90      36          50          不及格",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Python</th>\n      <th>Java</th>\n      <th>H5</th>\n      <th>Pandas</th>\n      <th>Python_new</th>\n      <th>Python_grade</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>鲁班</th>\n      <td>57</td>\n      <td>92</td>\n      <td>81</td>\n      <td>24</td>\n      <td>49</td>\n      <td>不及格</td>\n    </tr>\n    <tr>\n      <th>张三丰</th>\n      <td>33</td>\n      <td>42</td>\n      <td>0</td>\n      <td>98</td>\n      <td>25</td>\n      <td>不及格</td>\n    </tr>\n    <tr>\n      <th>张无忌</th>\n      <td>47</td>\n      <td>3</td>\n      <td>39</td>\n      <td>28</td>\n      <td>39</td>\n      <td>不及格</td>\n    </tr>\n    <tr>\n      <th>杜甫</th>\n      <td>23</td>\n      <td>20</td>\n      <td>20</td>\n      <td>59</td>\n      <td>15</td>\n      <td>不及格</td>\n    </tr>\n    <tr>\n      <th>李白</th>\n      <td>58</td>\n      <td>83</td>\n      <td>90</td>\n      <td>36</td>\n      <td>50</td>\n      <td>不及格</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[\"Python\"] = df[\"Python\"].apply(lambda  x:x+10)\n",
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "outputs": [
    {
     "data": {
      "text/plain": "     Python  Java  H5  Pandas\n鲁班       57    92  81      24\n张三丰      33    42   0      98\n张无忌      47     3  39      28\n杜甫       23    20  20      59\n李白       58    83  90      36",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Python</th>\n      <th>Java</th>\n      <th>H5</th>\n      <th>Pandas</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>鲁班</th>\n      <td>57</td>\n      <td>92</td>\n      <td>81</td>\n      <td>24</td>\n    </tr>\n    <tr>\n      <th>张三丰</th>\n      <td>33</td>\n      <td>42</td>\n      <td>0</td>\n      <td>98</td>\n    </tr>\n    <tr>\n      <th>张无忌</th>\n      <td>47</td>\n      <td>3</td>\n      <td>39</td>\n      <td>28</td>\n    </tr>\n    <tr>\n      <th>杜甫</th>\n      <td>23</td>\n      <td>20</td>\n      <td>20</td>\n      <td>59</td>\n    </tr>\n    <tr>\n      <th>李白</th>\n      <td>58</td>\n      <td>83</td>\n      <td>90</td>\n      <td>36</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.loc[:,\"Python\":\"Python_new\"]\n",
    "df = df.iloc[:,:-1]\n",
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "outputs": [
    {
     "data": {
      "text/plain": "     Python  Java   H5  Pandas\n鲁班       67   102   91      34\n张三丰      43    52   10     108\n张无忌      57    13   49      38\n杜甫       33    30   30      69\n李白       68    93  100      46",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Python</th>\n      <th>Java</th>\n      <th>H5</th>\n      <th>Pandas</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>鲁班</th>\n      <td>67</td>\n      <td>102</td>\n      <td>91</td>\n      <td>34</td>\n    </tr>\n    <tr>\n      <th>张三丰</th>\n      <td>43</td>\n      <td>52</td>\n      <td>10</td>\n      <td>108</td>\n    </tr>\n    <tr>\n      <th>张无忌</th>\n      <td>57</td>\n      <td>13</td>\n      <td>49</td>\n      <td>38</td>\n    </tr>\n    <tr>\n      <th>杜甫</th>\n      <td>33</td>\n      <td>30</td>\n      <td>30</td>\n      <td>69</td>\n    </tr>\n    <tr>\n      <th>李白</th>\n      <td>68</td>\n      <td>93</td>\n      <td>100</td>\n      <td>46</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.apply(lambda x:x+10)\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "outputs": [
    {
     "data": {
      "text/plain": "     Python  Java  H5  Pandas\n鲁班       57    92  81      24\n张三丰      33    42   0      98\n张无忌      47     3  39      28\n杜甫       23    20  20      59\n李白       58    83  90      36",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Python</th>\n      <th>Java</th>\n      <th>H5</th>\n      <th>Pandas</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>鲁班</th>\n      <td>57</td>\n      <td>92</td>\n      <td>81</td>\n      <td>24</td>\n    </tr>\n    <tr>\n      <th>张三丰</th>\n      <td>33</td>\n      <td>42</td>\n      <td>0</td>\n      <td>98</td>\n    </tr>\n    <tr>\n      <th>张无忌</th>\n      <td>47</td>\n      <td>3</td>\n      <td>39</td>\n      <td>28</td>\n    </tr>\n    <tr>\n      <th>杜甫</th>\n      <td>23</td>\n      <td>20</td>\n      <td>20</td>\n      <td>59</td>\n    </tr>\n    <tr>\n      <th>李白</th>\n      <td>58</td>\n      <td>83</td>\n      <td>90</td>\n      <td>36</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "outputs": [
    {
     "data": {
      "text/plain": "Python    47.0\nJava      42.0\nH5        39.0\nPandas    36.0\ndtype: float64"
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.apply(lambda x : x.median(), axis=1)  # 行的中位数\n",
    "df.apply(lambda x : x.median(), axis=0)  # 行的中位数"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "outputs": [
    {
     "data": {
      "text/plain": "   Python  NumPy  Pandas\nA       0      9       0\nB       7      5       2\nC       7      2       7\nD       4      4       6\nE       7      7       0\nF       0      9       5\nH       1      9       8\nI       0      3       8\nJ       5      4       0\nK       9      4       6",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Python</th>\n      <th>NumPy</th>\n      <th>Pandas</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>A</th>\n      <td>0</td>\n      <td>9</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>B</th>\n      <td>7</td>\n      <td>5</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>C</th>\n      <td>7</td>\n      <td>2</td>\n      <td>7</td>\n    </tr>\n    <tr>\n      <th>D</th>\n      <td>4</td>\n      <td>4</td>\n      <td>6</td>\n    </tr>\n    <tr>\n      <th>E</th>\n      <td>7</td>\n      <td>7</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>F</th>\n      <td>0</td>\n      <td>9</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>H</th>\n      <td>1</td>\n      <td>9</td>\n      <td>8</td>\n    </tr>\n    <tr>\n      <th>I</th>\n      <td>0</td>\n      <td>3</td>\n      <td>8</td>\n    </tr>\n    <tr>\n      <th>J</th>\n      <td>5</td>\n      <td>4</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>K</th>\n      <td>9</td>\n      <td>4</td>\n      <td>6</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame(data=np.random.randint(0, 10, size=(10,3)),\n",
    "                  index=list('ABCDEFHIJK'),\n",
    "                  columns=['Python', 'NumPy', 'Pandas'])\n",
    "\n",
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "outputs": [
    {
     "data": {
      "text/plain": "   Python  NumPy\nA       0     90\nB      49     50\nC      49     20\nD      16     40\nE      49     70\nF       0     90\nH       1     90\nI       0     30\nJ      25     40\nK      81     40",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>Python</th>\n      <th>NumPy</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>A</th>\n      <td>0</td>\n      <td>90</td>\n    </tr>\n    <tr>\n      <th>B</th>\n      <td>49</td>\n      <td>50</td>\n    </tr>\n    <tr>\n      <th>C</th>\n      <td>49</td>\n      <td>20</td>\n    </tr>\n    <tr>\n      <th>D</th>\n      <td>16</td>\n      <td>40</td>\n    </tr>\n    <tr>\n      <th>E</th>\n      <td>49</td>\n      <td>70</td>\n    </tr>\n    <tr>\n      <th>F</th>\n      <td>0</td>\n      <td>90</td>\n    </tr>\n    <tr>\n      <th>H</th>\n      <td>1</td>\n      <td>90</td>\n    </tr>\n    <tr>\n      <th>I</th>\n      <td>0</td>\n      <td>30</td>\n    </tr>\n    <tr>\n      <th>J</th>\n      <td>25</td>\n      <td>40</td>\n    </tr>\n    <tr>\n      <th>K</th>\n      <td>81</td>\n      <td>40</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#transform()()函数：既支持 Series，也支持 DataFrame\n",
    "def convert(x):\n",
    "    if x.mean() > 5:\n",
    "        x *= 10\n",
    "    else:\n",
    "        x *= -10\n",
    "    return x\n",
    "df.transform({\"Python\":np.square,\"NumPy\":np.square,\"NumPy\":convert})"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "outputs": [
    {
     "data": {
      "text/plain": "       sqrt          exp\nA  1.732051    20.085537\nB  2.236068   148.413159\nC  0.000000     1.000000\nD  2.000000    54.598150\nE  0.000000     1.000000\nF  1.414214     7.389056\nH  1.414214     7.389056\nI  2.236068   148.413159\nJ  2.828427  2980.957987\nK  2.236068   148.413159",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>sqrt</th>\n      <th>exp</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>A</th>\n      <td>1.732051</td>\n      <td>20.085537</td>\n    </tr>\n    <tr>\n      <th>B</th>\n      <td>2.236068</td>\n      <td>148.413159</td>\n    </tr>\n    <tr>\n      <th>C</th>\n      <td>0.000000</td>\n      <td>1.000000</td>\n    </tr>\n    <tr>\n      <th>D</th>\n      <td>2.000000</td>\n      <td>54.598150</td>\n    </tr>\n    <tr>\n      <th>E</th>\n      <td>0.000000</td>\n      <td>1.000000</td>\n    </tr>\n    <tr>\n      <th>F</th>\n      <td>1.414214</td>\n      <td>7.389056</td>\n    </tr>\n    <tr>\n      <th>H</th>\n      <td>1.414214</td>\n      <td>7.389056</td>\n    </tr>\n    <tr>\n      <th>I</th>\n      <td>2.236068</td>\n      <td>148.413159</td>\n    </tr>\n    <tr>\n      <th>J</th>\n      <td>2.828427</td>\n      <td>2980.957987</td>\n    </tr>\n    <tr>\n      <th>K</th>\n      <td>2.236068</td>\n      <td>148.413159</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[\"Python\"].transform([np.sqrt, np.exp])\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}